﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTcms.Common;

namespace DTcms.Web.admin.company
{
    public partial class company_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString();//操作类型
        private int id = 0;

        protected string _user_name;
        protected string _mobile;
        protected string _avatar;
        protected int _user_id = 0;
        public int company_id = 0;
        public string video_url = "";
        public bool is_sort = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");
            this._user_name = DTRequest.GetQueryString("user_name");
            this._mobile = DTRequest.GetQueryString("mobile");
            this._avatar = DTRequest.GetQueryString("avatar");
            this._user_id = DTRequest.GetQueryInt("user_id", 0);
            
            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                company_id = this.id;
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back");
                    return;
                }
                if (!new BLL.company().Exists(this.id))
                {
                    JscriptMsg("记录不存在或已被删除！", "back");
                    return;
                }

                Model.manager man = GetAdminInfo();
                if (man != null) {
                    if (man.role_id == 1) { is_sort = true; }
                }
            }
            if (!Page.IsPostBack)
            {
                GetProvince();
                GetCategoryList();
                GetRzCategory();
                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
                else
                {
                    txtTitle.Text = this._user_name;
                    txtMobile.Text = this._mobile;
                    txtLogo.Text = this._avatar;
                }
            }
        }

        #region 绑定类别=================================
        private void GetCategoryList()
        {
            BLL.company_category bll = new BLL.company_category();
            DataTable dt = bll.GetList(0, "", "id asc").Tables[0];

            this.ddlCategoryId.Items.Clear();
            this.ddlCategoryId.Items.Add(new ListItem("选择所属分类", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["title"].ToString().Trim();

                this.ddlCategoryId.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 绑定省份=================================
        private void GetProvince()
        {
            BLL.province bll = new BLL.province();
            DataTable dt = bll.GetList(0, "", "id asc").Tables[0];

            this.ddlProvince.Items.Clear();
            this.ddlProvince.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["province_name"].ToString().Trim();

                this.ddlProvince.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 绑定城市=================================
        private void GetCity(int province_id)
        {
            BLL.city bll = new BLL.city();
            DataTable dt = bll.GetList(0, "province_id=" + province_id, "id asc").Tables[0];

            this.ddlCity.Items.Clear();
            this.ddlCity.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["city_name"].ToString().Trim();

                this.ddlCity.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 绑定城市=================================
        private void GetDistrict(int city_id)
        {
            BLL.district bll = new BLL.district();
            DataTable dt = bll.GetList(0, "city_id=" + city_id, "id asc").Tables[0];

            this.ddlArea.Items.Clear();
            this.ddlArea.Items.Add(new ListItem("选择省份", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["district_name"].ToString().Trim();

                this.ddlArea.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 绑定认证类别=================================
        private void GetRzCategory()
        {
            BLL.rz_category bll = new BLL.rz_category();
            DataTable dt = bll.GetList(0, "", "sort_id asc,id asc").Tables[0];

            this.ddlRzCategoryID.Items.Clear();
            this.ddlRzCategoryID.Items.Add(new ListItem("不属于认证企业", "0"));
            foreach (DataRow dr in dt.Rows)
            {
                string Id = dr["id"].ToString();
                string Title = dr["title"].ToString().Trim();
                this.ddlRzCategoryID.Items.Add(new ListItem(Title, Id));
            }
        }
        #endregion

        #region 赋值操作=================================
        private void ShowInfo(int _id)
        {
            BLL.company bll = new BLL.company();
            Model.company model = bll.GetModel(_id);

            rblStatus.SelectedValue = model.status.ToString();
            txtTitle.Text = model.title;
            txtCallIndex.Text = model.call_index;
            ddlCategoryId.SelectedValue = model.category_id.ToString();
            txtProduct.Text = model.pro_remark;
            txtLogo.Text = model.logo;
            txtLianxiren.Text = model.lianxiren;
            txtTel.Text = model.telphone;
            txtMobile.Text = model.mobile;
            txtMobile.Attributes.Remove("ajaxurl");
            txtwebsite.Text = model.website;
            ddlProvince.SelectedValue = model.province_id.ToString();
            if (model.province_id > 0)
            {
                GetCity(model.province_id);
            }
            ddlCity.SelectedValue = model.city_id.ToString();
            if (model.city_id > 0)
            {
                GetDistrict(model.city_id);
            }
            ddlArea.SelectedValue = model.district_id.ToString();

            lbQuanziName.Text = new BLL.quanzi().GetTitle(model.quanzi_id);
            txtQuanziID.Text = model.quanzi_id.ToString();

            txtUserID.Text = model.user_id.ToString();
            if (model.user_id > 0)
            {
                lbUserName.Text = new BLL.users().GetUserName(model.user_id);
            }

            txtAddress.Text = model.address;
            ddlRzCategoryID.SelectedValue = model.rz_category_id.ToString();
            txtRzImgUrl.Text = model.rz_imgurl;
            txtContent.Value = model.about;
            txtZuobiao.Text = model.zuobiao;
            txtTopImg.Text = model.topimg;

            //2018-12-06 Demon 添加经营范围
            txtbusinessScope.Text = model.businessScope;
            txtsort_id.Text = model.Sort_id.ToString();

            //2019-02-11 添加视频
            video_url = Request.Url.Scheme + "://" + Request.Url.Host + ":" + Request.Url.Port  + model.video_url;
            txtvideo_img.Text = model.video_img;
            txtvideo.Text = model.video_url;
            //Response.Write(video_url);
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            Model.company model = new Model.company();
            BLL.company bll = new BLL.company();

            model.user_id = this._user_id;
            model.title = txtTitle.Text.Trim();
            model.category_id = Utils.StrToInt(ddlCategoryId.SelectedValue, 0);
            model.logo = "pic.shicaiquanzi.com" + txtLogo.Text.Trim();
            model.pro_remark = txtProduct.Text.Trim();
            model.add_time = DateTime.Now;
            model.lianxiren = txtLianxiren.Text.Trim();
            model.telphone = txtTel.Text.Trim();
            model.mobile = txtMobile.Text.Trim();

            model.province_id = Utils.StrToInt(Request.Form["ddlProvince"], 0);
            model.city_id = Utils.StrToInt(Request.Form["ddlCity"], 0);
            model.district_id = Utils.StrToInt(Request.Form["ddlArea"], 0);

            model.quanzi_id = Utils.StrToInt(txtQuanziID.Text.Trim(), 0);
            model.user_id = Utils.StrToInt(txtUserID.Text.Trim(), 0);

            model.address = txtAddress.Text.Trim();
            model.rz_category_id = int.Parse(ddlRzCategoryID.SelectedValue);
            model.rz_imgurl = txtRzImgUrl.Text.Trim();
            model.status = Utils.StrToInt(rblStatus.SelectedValue, 1);
            model.rz_status = 1;
            model.call_index = txtCallIndex.Text.Trim();
            model.about = txtContent.Value;
            model.zuobiao = txtZuobiao.Text;
            model.topimg = txtTopImg.Text.Trim();
            //2018-12-06 Demon 添加经营范围
            model.businessScope = txtbusinessScope.Text;
            model.Sort_id = Utils.StrToInt(txtsort_id.Text, 0);
            model.website = txtwebsite.Text;
            ///2019-02-11 Demon 添加视频
            model.video_img = "";

            if (txtvideo.Text.Trim() + "" != "") { model.video_url = txtvideo.Text.Trim(); }
            //txtvideo_img
            if (txtvideo_img.Text.Trim() + "" != "") { model.video_img = txtvideo_img.Text.Trim(); }

            int result = bll.Add(model);

            if (result > 0)
            {
                string _pwd = Utils.GetRamCode();

                int new_user_id = new BLL.users().Add(txtTitle.Text.Trim(), _pwd, model.mobile, model.logo.ToString(), model.province_id, model.city_id, model.district_id, result);
                if (new_user_id > 0)
                {
                    bll.UpdateField(result, "user_id=" + new_user_id);
                    //同步 店铺logo 到会员头像
                    BLL.users userbll = new BLL.users();
                    userbll.UpdateField(model.user_id, "company_id='"+model.id+"',avatar='" + model.logo + "'");
                }
                return true;
            }

            return false;
        }
        #endregion

        #region 修改操作=================================
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.company bll = new BLL.company();
            Model.company model = bll.GetModel(_id);
            model.call_index = txtCallIndex.Text.Trim();
            model.title = txtTitle.Text.Trim();
            model.category_id = Utils.StrToInt(ddlCategoryId.SelectedValue, 0);
            if (txtLogo.Text.Trim().Contains("shicaiquanzi"))
            {
                model.logo = txtLogo.Text.Trim();
            }
            else
            {
                model.logo = "pic.shicaiquanzi.com" + txtLogo.Text.Trim();
            }
            model.pro_remark = txtProduct.Text.Trim();
            model.lianxiren = txtLianxiren.Text.Trim();
            model.telphone = txtTel.Text.Trim();
            model.mobile = txtMobile.Text.Trim();

            model.province_id = Utils.StrToInt(Request.Form["ddlProvince"], 0);
            model.city_id = Utils.StrToInt(Request.Form["ddlCity"], 0);
            model.district_id = Utils.StrToInt(Request.Form["ddlArea"], 0);

            model.quanzi_id = Utils.StrToInt(txtQuanziID.Text.Trim(), 0);
            model.user_id = Utils.StrToInt(txtUserID.Text.Trim(), 0);

            model.address = txtAddress.Text.Trim();
            model.rz_category_id = int.Parse(ddlRzCategoryID.SelectedValue);
            model.rz_imgurl = txtRzImgUrl.Text.Trim();
            model.topimg = txtTopImg.Text.Trim();
            model.status = Utils.StrToInt(rblStatus.SelectedValue, 1);
            model.about = txtContent.Value;
            model.zuobiao = txtZuobiao.Text;
            //2018-12-06 Demon 添加经营范围
            model.businessScope = txtbusinessScope.Text;
            if (is_sort) { model.Sort_id = Utils.StrToInt(txtsort_id.Text, 0); }
            
            model.website = txtwebsite.Text;


            ///2019-02-11 Demon 添加视频
            model.video_img = "";
            model.video_url = txtvideo.Text.Trim(); 
            model.video_img = txtvideo_img.Text.Trim(); 

            //同步 店铺logo 到会员头像
            BLL.users userbll = new BLL.users();
            //userbll.UpdateField(model.user_id, "avatar='" + model.logo + "'");
            
            userbll.UpdateField(model.user_id, "company_id='" + model.id + "',avatar='" + model.logo + "'");
            if (bll.Update(model))
            {
                result = true;
            }

            return result;
        }
        #endregion

        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("修改企业黄页信息成功！", "company_list.aspx", "parent.loadMenuTree");
            }
            else //添加
            {
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误！", "");
                    return;
                }
                JscriptMsg("添加企业黄页信息成功！", "company_list.aspx", "parent.loadMenuTree");
            }
        }
    }
}